package com.lcredu.dao.impl;

import com.lcredu.dao.BasicDAO;
import com.lcredu.dao.FurnDAO;
import com.lcredu.entity.Furn;

import java.util.Collections;
import java.util.List;

/**
 * @author 黎长容
 * @date 2025-07-06 11:09
 */
public class FurnDAOImpl extends BasicDAO<Furn> implements FurnDAO {

    @Override
    public List<Furn> getFurnList() {
        String sql = "select id, name, img_path AS imgPath, maker, price, sales, stock from furn;";
        return queryMulti(sql, Furn.class);
    }

    @Override
    public int add(Furn furn) {
        String addSql = "insert into furn(id, name, img_path, maker, price, sales, stock) values (?,?,?,?,?,?,?)";
        return update(addSql, furn.getId(), furn.getName(), furn.getimgPath(),
                furn.getMaker(), furn.getPrice(), furn.getSales(), furn.getStock());
    }

    @Override
    public int del(int id) {
        String delSql = "delete from furn where id = ?";
        return update(delSql, id);
    }

    @Override
    public Furn selectFurnById(int furnId) {
        String selSql = "select id, name, img_path AS imgPath, maker, price, sales, stock from furn where id = ?";
        return querySingle(selSql, Furn.class, furnId);
    }

    @Override
    public int updateFurn(Furn furn) {
        String updateSql = "updatex furn set name = ?,img_path = ?,maker = ?,price=?,sales=?,stock = ? where id = ?";
        return update(updateSql, furn.getName(), furn.getimgPath(), furn.getMaker(), furn.getPrice(), furn.getSales(), furn.getStock(), furn.getId());
    }

    @Override
    public int getTotalRow() {
        String selSql = "select count(*) from furn";
        return ((Number) queryScalar(selSql)).intValue();
    }

    @Override
    public List<Furn> getItems(Integer begin, Integer pageSize) {
        String selItemsSql = "select id, name, img_path AS imgPath, maker, price, sales, stock from furn limit ?,?";
        List<Furn> furns = queryMulti(selItemsSql, Furn.class, begin, pageSize);
        return furns;
    }

    @Override
    public int getTotalRowByName(String name) {
        String selSql = "select count(name) from furn where name like ?";
        //黎长容知识8 如何返回一个复杂的int
        return ((Number) queryScalar(selSql, "%" + name + "%")).intValue();
    }

    @Override
    public List<Furn> getItemsByName(Integer begin, Integer pageSize,String name) {
        String selItemsByNameSql = "select id, name, img_path AS imgPath, maker, price, sales, stock from furn where name like ? limit ?,?";
        return queryMulti(selItemsByNameSql, Furn.class, "%" + name + "%", begin, pageSize);
    }


}
